package leetcode.t5Stack;

import java.util.Stack;

/**
 * 20. 有效的括号
 *
 * @author K
 * @date 2024/1/19 15:01
 */
public class T20_IsValid {
    public static boolean isValid(String s) {
        Stack<String> stack = new Stack<>();
        for (int i = 0; i < s.length(); i++) {
            String cur = String.valueOf(s.charAt(i));
            if (stack.isEmpty()) {
                stack.add(cur);
            } else {
                String his = stack.peek();
                if (cur.equals(convert(his))) {
                    stack.pop();
                } else {
                    stack.add(cur);
                }
            }
        }

        if (!stack.isEmpty()) {
            return false;
        }

        return true;
    }

    public static String convert(String str) {
        if ("(".equals(str)) {
            return ")";
        } else if ("[".equals(str)) {
            return "]";
        } else if ("{".equals(str)) {
            return "}";
        }
        return null;
    }

    public static void main(String[] args) {
        boolean valid = isValid("()");
        boolean valid1 = isValid("()[]{}");
        boolean valid2 = isValid("()[]{");
        boolean valid3 = isValid("(]");
        boolean valid4 = isValid("{[]}");
        System.out.println();
    }
}
